View Javadoc

1   /*
2   Copyright 2011-2014 AGH-UST, http://www.agh.edu.pl
3   Faculty of Computer Science, Electronics and Telecommunications
4   Department of Computer Science 
5   
6   See the NOTICE file distributed with this work for additional
7   information regarding copyright ownership
8   
9   Licensed under the Apache License, Version 2.0 (the "License");
10  you may not use this file except in compliance with the License.
11  You may obtain a copy of the License at
12  
13    http://www.apache.org/licenses/LICENSE-2.0
14  
15  Unless required by applicable law or agreed to in writing, software
16  distributed under the License is distributed on an "AS IS" BASIS,
17  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  See the License for the specific language governing permissions and
19  limitations under the License.
20  */
21  package org.universAAL.ri.gateway.eimanager.impl;
22  
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.osgi.framework.ServiceReference;
28  import org.universAAL.middleware.container.SharedObjectListener;
29  import org.universAAL.ri.gateway.communicator.Activator;
30  import org.universAAL.ri.gateway.eimanager.ExportOperationInterceptor;
31  import org.universAAL.ri.gateway.eimanager.ImportOperationInterceptor;
32  import org.universAAL.ri.gateway.eimanager.exception.InterruptExecutionException;
33  import org.universAAL.ri.gateway.eimanager.impl.importing.ImportRequest;
34  
35  public enum EIOperationManager implements SharedObjectListener{
36  	Instance;
37  	
38  	private ImportInterceptorsComparator importComparator;
39  	private ExportInterceptorsComparator exportComparator;
40  	private List<ImportOperationInterceptor> importList;
41  	private List<ExportOperationInterceptor> exportList;
42  	
43  	{
44  		importComparator = new ImportInterceptorsComparator();
45  		exportComparator = new ExportInterceptorsComparator();
46  		
47  		importList = new ArrayList<ImportOperationInterceptor>();
48  		exportList = new ArrayList<ExportOperationInterceptor>();
49  	}
50  	
51  	public void init(){
52  			Activator.mc.getContainer()
53  					.fetchSharedObject(Activator.mc ,
54  						new Object[] { ImportOperationInterceptor.class.getName() }, this);
55  
56  			Activator.mc.getContainer()
57  					.fetchSharedObject(Activator.mc ,
58  						new Object[] { ExportOperationInterceptor.class.getName() }, this);
59  	}
60  	
61  	public void executeExportOperationChain(ImportRequest request, Type type) throws InterruptExecutionException{
62  		List<ExportOperationInterceptor> interceptors = exportList;
63  		Collections.sort(interceptors, exportComparator);
64  		for(ExportOperationInterceptor i : interceptors){
65  			i.process(request);
66  		}
67  	}
68  	
69  	public void executeImportOperationChain(ImportRequest request, Type type) throws InterruptExecutionException{
70  		List<ImportOperationInterceptor> interceptors = importList;
71  		Collections.sort(interceptors, importComparator);
72  		for(ImportOperationInterceptor i : interceptors){
73  			i.process(request);
74  		}
75  	}
76  	
77  	public enum Type {
78  		Service,Context,UI;
79  	}
80  
81  	public void sharedObjectAdded(Object sharedObj, Object removeHook) {
82  		if (sharedObj instanceof ImportOperationInterceptor){
83  			importList.add((ImportOperationInterceptor) sharedObj);
84  		}else if (sharedObj instanceof ExportOperationInterceptor){
85  			exportList.add((ExportOperationInterceptor) sharedObj);
86  		}
87  	}
88  
89  	public void sharedObjectRemoved(Object removeHook) {
90  		if (removeHook instanceof ServiceReference){
91  			ServiceReference ref = (ServiceReference)removeHook;
92  			Object removedService = Activator.bc.getService(ref);
93  			if (removedService instanceof ImportOperationInterceptor){
94  				importList.add((ImportOperationInterceptor) removedService);
95  			}else if (removedService instanceof ExportOperationInterceptor){
96  				exportList.add((ExportOperationInterceptor) removedService);
97  			}
98  		}
99  	}
100 }